﻿<?xml version="1.0" ?>
<xsd:schema
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified">

    <xsd:element name="root">
        <xsd:complexType >
            <xsd:choice minOccurs="0" maxOccurs="unbounded" >
                <xsd:element name="module" type="moduleType" />
                <xsd:element name="include" type="includeType" />
            </xsd:choice>
        </xsd:complexType>
    </xsd:element>

    <xsd:complexType name="moduleType" >
        <xsd:sequence minOccurs="0" maxOccurs="1" >
            <xsd:element name="table" type="tableType" minOccurs="0" maxOccurs="unbounded" />
            <xsd:choice minOccurs="0" maxOccurs="unbounded" >
                <xsd:element name="statement" type="statementType" />
                <xsd:element name="stmt" type="statementType" />
            </xsd:choice>
        </xsd:sequence>
        <xsd:attribute name="name" type="identityName" use="required" />
        <xsd:attribute name="package" type="identityName" use="optional" />
        <xsd:attribute name="description" type="xsd:normalizedString" use="optional" default="" />
    </xsd:complexType>

    <xsd:complexType name="includeType" >
        <xsd:attribute name="src" type="xsd:string" use="required" />
    </xsd:complexType>

    <xsd:complexType name="tableType" >
        <xsd:choice minOccurs="1" maxOccurs="unbounded" >
            <xsd:element name="column" type="columnType" />
        </xsd:choice>
        <xsd:attribute name="name" type="identityName" use="required" />
        <xsd:attribute name="description" type="xsd:normalizedString" use="optional" default="" />
    </xsd:complexType>

    <xsd:complexType name="columnType" >
        <xsd:attribute name="name" type="identityName" use="required" />
        <xsd:attribute name="size" type="columnSizeType" use="optional" default="0" />
        <xsd:attribute name="type" type="columnTypeType" use="optional" default="string" />
        <xsd:attribute name="unique" type="boolean" use="optional" default="false" />
        <xsd:attribute name="primaryKey" type="boolean" use="optional" default="false" />
        <xsd:attribute name="description" type="xsd:normalizedString" use="optional" default="" />
        <xsd:attribute name="defaultValue" type="xsd:normalizedString" use="optional" />
    </xsd:complexType>

    <xsd:complexType name="statementType" mixed="true" >
        <xsd:sequence>
            <xsd:element name="dynamic" type="dynamicStatementType" minOccurs="0" maxOccurs="unbounded" />
            <xsd:element name="parameter" type="parameterType" minOccurs="0" maxOccurs="unbounded" />
        </xsd:sequence>
        <xsd:attribute name="name" type="identityName" use="required" />
        <xsd:attribute name="allowPaging" type="boolean" default="auto" />
        <xsd:attribute name="commandType" type="statementCommandTypeType" default="text" />
        <xsd:attribute name="resultType" type="statementResultTypeType" default="updated-count" />
        <xsd:attribute name="resultTable" type="identityName" use="optional" />
        <xsd:attribute name="isReadOnly" type="boolean" default="auto" />
        <xsd:attribute name="description" type="xsd:normalizedString" use="optional" default="" />
    </xsd:complexType>

    <xsd:simpleType name="statementCommandTypeType" >
        <xsd:restriction base="xsd:normalizedString" >
            <xsd:enumeration value="text" />
            <!-- store procedure //-->
            <xsd:enumeration value="sp" />
        </xsd:restriction>
    </xsd:simpleType>

    <xsd:complexType name="dynamicStatementType" >
        <xsd:choice minOccurs="1" maxOccurs="unbounded">
            <xsd:element name="exists" type="dynamicStatementTextType" />
            <xsd:element name="notExists" type="dynamicStatementTextType" />
            <xsd:element name="isNull" type="dynamicStatementTextType" />
            <xsd:element name="isNotNull" type="dynamicStatementTextType" />
            <xsd:element name="isEmpty" type="dynamicStatementTextType" />
            <xsd:element name="isNotEmpty" type="dynamicStatementTextType" />
            <xsd:element name="isNullOrEmpty" type="dynamicStatementTextType" />
        </xsd:choice>
        <xsd:attribute name="prepend" type="xsd:normalizedString" use="optional" />
    </xsd:complexType>

    <xsd:complexType name="dynamicStatementTextType" mixed="true" >
        <xsd:attribute name="parameter" type="identityName" use="required" />
    </xsd:complexType>

    <xsd:complexType name="parameterType" >
        <xsd:attribute name="name" type="identityName" use="required" />
        <xsd:attribute name="type" type="columnTypeType" use="required" />
        <xsd:attribute name="matchType" type="parameterMatchType" use="optional" default="exact" />
        <xsd:attribute name="direction" type="directionType" use="optional" default="input" />
        <xsd:attribute name="size" type="columnSizeType" use="optional" default="0" >
        </xsd:attribute>
        <xsd:attribute name="description" type="xsd:normalizedString" use="optional" default="" />
    </xsd:complexType>

    <xsd:simpleType name="columnSizeType" >
        <xsd:restriction base="xsd:nonNegativeInteger" >
            <xsd:minInclusive fixed="true" value="0" />
            <xsd:maxInclusive fixed="true" value="4000" />
            <xsd:whiteSpace value="collapse" />
        </xsd:restriction>
    </xsd:simpleType>

    <xsd:simpleType name="parameterMatchType" >
        <xsd:restriction base="xsd:normalizedString" >
            <xsd:enumeration value="exact" />
            <xsd:enumeration value="starts-with" />
            <xsd:enumeration value="ends-with" />
            <xsd:enumeration value="contains" />
        </xsd:restriction>
    </xsd:simpleType>

    <xsd:simpleType name="directionType" >
        <xsd:restriction base="xsd:normalizedString">
            <xsd:enumeration value="input" />
            <xsd:enumeration value="output" />
            <xsd:enumeration value="inputOutput" />
        </xsd:restriction>
    </xsd:simpleType>

    <xsd:simpleType name="columnTypeType" >
        <xsd:restriction base="xsd:normalizedString" >
            <xsd:enumeration value="string" />
            <xsd:enumeration value="int" />
            <xsd:enumeration value="integer" />
            <xsd:enumeration value="long" />            
            <xsd:enumeration value="money" />
            <xsd:enumeration value="decimal" />
            <xsd:enumeration value="currency" />
            <xsd:enumeration value="datetime" />
            <xsd:enumeration value="date" />
            <xsd:enumeration value="endDate" />
            <xsd:enumeration value="endMonth" />
            <xsd:enumeration value="endYear" />
            <xsd:enumeration value="float" />
            <xsd:enumeration value="double" />
            <xsd:enumeration value="bit" />
            <xsd:enumeration value="bool" />
            <xsd:enumeration value="boolean" />
        </xsd:restriction>
    </xsd:simpleType>

    <xsd:simpleType name="statementResultTypeType" >
        <xsd:restriction base="xsd:normalizedString" >
            <!-- Table -->
            <xsd:enumeration value="table" />
            <!-- Scalar -->
            <xsd:enumeration value="string" />
            <xsd:enumeration value="int" />
            <xsd:enumeration value="integer" />
            <xsd:enumeration value="short" />
            <xsd:enumeration value="long" />
            <xsd:enumeration value="money" />
            <xsd:enumeration value="decimal" />
            <xsd:enumeration value="currency" />
            <xsd:enumeration value="datetime" />
            <xsd:enumeration value="date" />
            <xsd:enumeration value="float" />
            <xsd:enumeration value="double" />
            <xsd:enumeration value="bit" />
            <xsd:enumeration value="bool" />
            <xsd:enumeration value="boolean" />
            <!-- List -->
            <xsd:enumeration value="list-string" />
            <xsd:enumeration value="list-int" />
            <xsd:enumeration value="list-integer" />
            <xsd:enumeration value="list-short" />
            <xsd:enumeration value="list-long" />
            <xsd:enumeration value="list-money" />
            <xsd:enumeration value="list-decimal" />
            <xsd:enumeration value="list-currency" />
            <xsd:enumeration value="list-datetime" />
            <xsd:enumeration value="list-date" />
            <xsd:enumeration value="list-float" />
            <xsd:enumeration value="list-double" />
            <xsd:enumeration value="list-bit" />
            <xsd:enumeration value="list-bool" />
            <xsd:enumeration value="list-boolean" />
            <!-- Update -->
            <xsd:enumeration value="uc" />
            <xsd:enumeration value="updated-count" />
        </xsd:restriction>
    </xsd:simpleType>

    <xsd:simpleType name="boolean" >
        <xsd:restriction base="xsd:string" >
            <xsd:enumeration value="auto" />
            <xsd:enumeration value="0" />
            <xsd:enumeration value="1" />
            <xsd:enumeration value="yes" />
            <xsd:enumeration value="no" />
            <xsd:enumeration value="true" />
            <xsd:enumeration value="false" />
        </xsd:restriction>
    </xsd:simpleType>

    <xsd:simpleType name="text" >
        <xsd:restriction base="xsd:string" >
            <xsd:minLength value="1" />
        </xsd:restriction>
    </xsd:simpleType>

    <xsd:simpleType name="identityName" >
        <xsd:restriction base="xsd:normalizedString" >
            <xsd:minLength value="1" />
            <xsd:pattern value="[\w_]+" />
        </xsd:restriction>
    </xsd:simpleType>
</xsd:schema>
